class TakequizController < ApplicationController
  def question
    @quiz =Quiz.find(params[:quiz_id])
    @questions = @quiz.questions.map {|q| q.id}
    session[:q] = @questions
    session[:quiz] = @quiz.id
    session[:score] = 0
    redirect_to :action => :ask
  end

  def ask
    @quiz = Quiz.find(session[:quiz])
    if session[:q] == []
      redirect_to :controller => :takequiz, :action => :complete
    else
      @question = Question.find(session[:q][0..2])
      session[:q] = session[:q].drop(3)
    end
  end

  def grade
    q = Question.find(params[:id])
    ans = q.answers
    button = params[:answer].to_i
    score = ans[button][1]
    session[:score] = session[:score] + score.to_i
    redirect_to :action => :ask
  end

  def complete
    ts = session[:score]
    @quiz = Quiz.find(session[:quiz])
    reg = /(\d+) (.*)/
    scoring = @quiz.scoring
    scoring.lines.each do |a|
      if (a =~ reg) == 0
        if $1.to_i >= ts
          @sd = $2
          break
        end
      end
    end
    # Logic to create a response object goes here
  end

end
